package ru.sgu.diploma.practic.service.impl;

import ru.sgu.diploma.practic.domain.model.Automat;
import ru.sgu.diploma.practic.domain.model.AutomatResult;
import ru.sgu.diploma.practic.errors.*;
import ru.sgu.diploma.practic.service.AutomatRunner;
import ru.sgu.diploma.practic.utils.AutomatUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * User: datmakin
 * Date: 14.03.11
 * Email: atmakin.dv@gmail.com
 */
public class AutomatRunnerImpl implements AutomatRunner {

    private Integer length = 16;

    public List<AutomatResult> run(Automat automat) throws FatalException {
        List<AutomatResult> result = new LinkedList<AutomatResult>();
        try {
            String[] alphabet = automat.getAlphabet();
            List<String[]> input = new LinkedList<String[]>();
            for(int i = 1; i <= length; i++)
                 input.addAll(AutomatUtils.calculateSequencies(alphabet, length));
            for (String[] invector : input) {
                String[] outvector = automat.translate(invector);
                AutomatResult aresult = new AutomatResult();
                aresult.setInputStr(invector);
                aresult.setOuputStr(outvector);
                result.add(aresult);
            }
        } catch (AlphabetExcephion e) {
            e.printStackTrace();
            throw new FatalException("alphabet exception");
        } catch (NextLetterException e) {
            e.printStackTrace();
            throw new FatalException("alphabet exception");
        } catch (Throwable e) {
            e.printStackTrace();
        }

        return result;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }
}
